<?php
namespace Admin\Model
{

    class Subpages extends \Api\Model\DB2
    {
        /**
         * 
         * @return array
         */
        public function GetSubpagesList()
        {
            $Q = $this->db->query("SELECT
                    `a`.`id`,
                    SUBSTR(`a`.`content`,1,50) as content_short,
                    `a`.`alias`,
                    SUBSTR(`a`.`title`,1,50) as title_short,
                    `a`.`special`,
                    `b`.`action`,
                    `b`.`direction`        
                FROM
                    `arch_subpages` as a
                LEFT JOIN
                    `arch_subpage_effect` as b
                ON  
                    `b`.`id`=`a`.`effect_id`                
                ");            
            return $Q->fetchAll();
         }
         
         /**
          * 
          * @param int page numer
          * @return array or false
          */
         public function GetSubpage($id)
         {
            $Q = $this->db->Prepare("SELECT * FROM `arch_subpages` WHERE `id`=:id");
            $Q->bindValue(':id',(int)$id,\PDO::PARAM_INT);            
            $Q->execute();
            
            if( false !== ($R = $Q->fetch()))
            {
                return $R;
            }
            return false;
         }
         
         /**
          * Update subpage content and title
          * @param int id - subpage identifier
          * @param array with: alias, content, title
          * @return bool
          */
         public function UpdateSubpage($id, array $data = array())
         {
            if(empty($data) && !is_numeric($id)) return false;
            
            
            $Q = $this->db->Prepare("UPDATE `arch_subpages` SET
                `title` = :title, `content`=:content, `alias`=:alias,
                `view_template`=:view, `absolute_view`=:absview  ,
                `background`=:background, `effect_id`=:effect
                WHERE 
                `id`=:id
                ");
                
            $Q->bindValue(':title',$data['title'], \PDO::PARAM_STR);            
            $Q->bindValue(':alias',$data['alias'], \PDO::PARAM_STR);            
            $Q->bindValue(':content',$data['content'], \PDO::PARAM_STR);
            $Q->bindValue(':view',$data['view'],\PDO::PARAM_STR);            
            $Q->bindValue(':effect',$data['effect'],\PDO::PARAM_STR);                        
            $Q->bindValue(':absview',$data['view_type'],\PDO::PARAM_STR);            
            $Q->bindValue(':background',$data['background'],\PDO::PARAM_STR);            
            $Q->bindValue(':id',$data['id'], \PDO::PARAM_STR);
            
            return ($Q->execute()===true);            
         }
         
         /**
          * Insert new subpage into database
          * @param array with: alias, content, title, etc.
          * @return bool
          */
         public function InsertSubpage(array $data = array())
         {
              if(empty($data)) return false;
            $Q = $this->db->prepare("INSERT INTO `arch_subpages` 
                VALUES(
                0, :alias, :title, :content, 'No', :effect, :view, :absview, 0, :background)  
                ");
                
                
            $Q->bindValue(':title',$data['title'], \PDO::PARAM_STR);            
            $Q->bindValue(':alias',$data['alias'], \PDO::PARAM_STR);            
            $Q->bindValue(':content',$data['content'], \PDO::PARAM_STR);
            $Q->bindValue(':effect',$data['effect'], \PDO::PARAM_STR);
            $Q->bindValue(':view',$data['view'],\PDO::PARAM_STR);            
            $Q->bindValue(':absview',$data['view_type'],\PDO::PARAM_STR);                        
            $Q->bindValue(':background',$data['background'],\PDO::PARAM_STR);                        
            
            return ($Q->execute()===true);    
         }
         
         /**
          * Return page id to matching page alias
          * @return int or false
          */
         public function GetIDByAlias($alias)
         {
            $Q = $this->db->prepare("SELECT id FROM `arch_subpages` WHERE `alias`=:alias LIMIT 1");
            $Q->bindValue(':alias',$alias,\PDO::PARAM_STR);            
            $Q->execute();
            
            if( ($row = $Q->fetch()) !== false)
            {
                return $row['id'];
            }
            return false;
         }
         
         /**
          * Delete subpage by id
          */
         public function DeleteById($id)
         {
            $Q = $this->db->prepare("DELETE FROM `arch_subpages` WHERE `id`=:id LIMIT 1");
            $Q->bindValue(':id',$id,\PDO::PARAM_INT);
            $Q->execute();
            
            return true;
         }
         
         /**
          * Return list of templates for selected module and controller
          * @param string $module - module name
          * @param string $controller - controller name
          *
          * @return array
         */
         public function TemplateList($module, $controller)
         {            
            return \Api\File::ls($module.'/view/'.$controller, 'tpl',true);
         }
         
         /**
          * List background folder
          *
          * @return array
          */
         public function BackgroundList()
         {
            return \Api\File::ls('public/img/backgrounds/','jpg',true);
         }
         
         /**
          * Return  effeect
          *
          * @return array
          */
         public function ReturnEffect()
         {
            $Q = $this->db->query("SELECT * FROM arch_subpage_effect");
            return $Q->fetchAll();
         }
    }
} 
?>